// pages/information/informationList/index.js
const api = require('../../../config/home.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    loadding:false,
    classify:null,
    classification:null,
    pageNum:1,
    total:0,
    informationList:[]
  },
  swipclick(e){
      const {
        id
      } = e.currentTarget.dataset
      wx.navigateTo({
        url: `/pages/infoDetail/index?id=${id}`
      });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options){
      this.setData({
        classify:options.classify,
        classification:options.classification,
      })
      wx.setNavigationBarTitle({
        title: (options.classifyName || '咨询' )+'列表'
      })
      this.getInformationList()
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({pageNum:this.data.pageNum+1})
    this.getInformationList();
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getInformationList() {
    if(this.data.total>0 && this.data.informationList.length>=this.data.total){
      this.setData({pageNum:this.data.pageNum-1})
      return
    }
    this.setData({loadding:true})
    wx.request({
      url: api.informationUrl,
      data: {
        classify: this.data.classify,
        classification: this.data.classification,
        pageNum: this.data.pageNum,
        pageSize: 20
      },
      method: 'GET',
      success: (res) => {
        this.setData({loadding:false})
        if (res.data.code === 200) {
          let objs = res.data.information.records || []
          this.data.total = res.data.information.total || 0
          let informationList = this.data.informationList||[]
          informationList = informationList.concat(objs)
          this.setData({
            informationList: informationList
          })
        }
      },
      fail:(err=>{
        console.log(err)
        this.setData({loadding:false})
      }),
    });
  }
})